<?php
// Init owncloud
OCP\JSON::checkLoggedIn();
OCP\JSON::callCheck();

require_once(OC_App::getAppPath('resumes') . "/lib/resumes.php");
require_once(OC_App::getAppPath('resumes') . "/lib/search.php");

$model = new PResumesModelSearch();

$term_id = JRequest::getInt('term_id', 0);

// remove this term_id from opening list 
$app = PFactory::getApplication();
$terms_open = $app->getUserState('terms_open', '');

// if it's in open list, re-first. It works because will add a delayed-kill-job. If it's refresh/reload, 
// the term_id will be enabled. By the time when kill job is invoked, it will be canceled 
if(!empty($terms_open)) {
   $terms_open = array_diff($terms_open, array($term_id)); 
   $app->setUserState('terms_open', $terms_open);
}

if ($model->killProcessByTermid($term_id)) {
   OCP\JSON::success();
} else {
   OCP\JSON::error();
}
